﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib.Test
{
    public class ForLMJ
    {
        public void TestCreateText()
        {
            var db = HostApplicationServices.WorkingDatabase;

            using (var trans = db.TransactionManager.StartTransaction())
            {
                var btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                if (btr == null)
                    return;

                CreateText(trans, btr, "123", 10, new Point3d(0, 0, 0), TextHorizontalMode.TextLeft, TextVerticalMode.TextVerticalMid);
                CreateText(trans, btr, "123", 10, new Point3d(0, -20, 0), TextHorizontalMode.TextMid, TextVerticalMode.TextVerticalMid);
                CreateText(trans, btr, "123", 10, new Point3d(0, -40, 0), TextHorizontalMode.TextRight, TextVerticalMode.TextVerticalMid);

                var ptTL = new Point3d(0, 10, 0);
                CreateLine(trans, btr, ptTL, ptTL + new Vector3d(150, 0, 0), LineWeight.LineWeight050);
                CreateLine(trans, btr, ptTL + new Vector3d(0, -20, 0), ptTL + new Vector3d(150, -20, 0), LineWeight.ByLayer);
                CreateLine(trans, btr, ptTL + new Vector3d(0, -40, 0), ptTL + new Vector3d(150, -40, 0), LineWeight.ByLayer);
                CreateLine(trans, btr, ptTL + new Vector3d(0, -60, 0), ptTL + new Vector3d(150, -60, 0), LineWeight.LineWeight050);
                CreateLine(trans, btr, ptTL, ptTL + new Vector3d(0, -60, 0), LineWeight.LineWeight050);
                CreateLine(trans, btr, ptTL + new Vector3d(150, 0, 0), ptTL + new Vector3d(150, -60, 0), LineWeight.LineWeight050);
                
                trans.Commit();
            }
        }

        /// <summary>
        /// 特别的，当触发函数不是以命令方式启动时，比如说在一个浮动窗口内启动时需要把所有写操作使用文档锁锁定。
        /// </summary>
        public void RunsInApp()
        {
            using (Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                TestCreateText();
            }
        }

        public ObjectId CreateText(
            Transaction trans,
            BlockTableRecord btr,
            string content,
            double textHeight, 
            Point3d position,
            TextHorizontalMode horMode,
            TextVerticalMode verMode)
        {
            if (string.IsNullOrEmpty(content))
                throw new ArgumentNullException($"{nameof(content)} is empty");

            if (textHeight < 0.0)
                throw new InvalidOperationException($"{nameof(textHeight)} is can not less then 0");

            var text = new DBText();
            text.Height = textHeight;
            text.TextString = content;
            text.HorizontalMode = horMode;
            text.VerticalMode = verMode;
            text.Position = position;
            text.AlignmentPoint = position;

            var id = btr.AppendEntity(text);
            trans.AddNewlyCreatedDBObject(text, true);

            return id;
        }

        public ObjectId CreateLine(
            Transaction trans,
            BlockTableRecord btr,
            Point3d start,
            Point3d end,
            LineWeight lineWeight)
        {
            var line = new Line(start, end);
            line.LineWeight = lineWeight;

            var id = btr.AppendEntity(line);
            trans.AddNewlyCreatedDBObject(line, true);

            return id;   
        }

    }
}
